package com.kulian.Signalling.distribution.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kulian.Signalling.core.security.SecurityUtils;
import com.kulian.Signalling.distribution.converter.AppConverter;
import com.kulian.Signalling.distribution.dao.AppDao;
import com.kulian.Signalling.distribution.mapper.AppMapper;
import com.kulian.Signalling.distribution.mode.App;
import com.kulian.Signalling.distribution.mode.Package;
import com.kulian.Signalling.distribution.mode.bo.AppBO;
import com.kulian.Signalling.distribution.query.AppPageQuery;
import com.kulian.Signalling.distribution.service.AppService;
import com.kulian.Signalling.distribution.utils.CodeGenerator;
import com.kulian.Signalling.distribution.utils.PathManager;
import com.kulian.Signalling.distribution.vo.AppPageVO;
import com.kulian.Signalling.distribution.vo.AppViewModel;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
public class AppServiceImpl extends ServiceImpl<AppMapper, App> implements AppService {

    @Resource
    private AppDao appDao;
    @Resource
    private PathManager pathManager;
    private final AppConverter appConverter;


    public com.kulian.Signalling.distribution.mode.App saves(com.kulian.Signalling.distribution.mode.App app) {
        com.kulian.Signalling.distribution.mode.App app1 = this.appDao.save(app);
        app1.getCurrentPackage();
        try {
            // 触发级联查询
            app1.getWebHookList().forEach(webHook -> {});
        } catch (Exception e) {
            e.printStackTrace();
        }
        return app1;
    }


    public List<AppViewModel> findAll() {
        Iterable<com.kulian.Signalling.distribution.mode.App> apps = this.appDao.findAll();
        List<AppViewModel> list = new ArrayList<>();
        for (com.kulian.Signalling.distribution.mode.App app : apps) {
            AppViewModel appViewModel = new AppViewModel(app, this.pathManager, false);
            list.add(appViewModel);
        }
        return list;
    }

    @Override
    public IPage<AppPageVO> getAppPage(AppPageQuery queryParams) {

        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        queryParams.setUid(SecurityUtils.getUserId().toString());
        Page<AppBO> page = new Page<>(pageNum, pageSize);
        // 查询数据
        Page<AppBO> userPage = this.baseMapper.getAppPage(page, queryParams);

        // 实体转换
        return appConverter.toPageVo(userPage);
    }

    public AppViewModel getById(String appID) {
        Optional<com.kulian.Signalling.distribution.mode.App> optionalApp = this.appDao.findById(appID);
        com.kulian.Signalling.distribution.mode.App app = optionalApp.get();
        if (app != null) {
            app.getPackageList().forEach(aPackage -> {});
            AppViewModel appViewModel = new AppViewModel(app, this.pathManager, true);
            return appViewModel;
        }
        return null;
    }


    public com.kulian.Signalling.distribution.mode.App getByPackage(Package aPackage) {
        com.kulian.Signalling.distribution.mode.App app = this.appDao.get(aPackage.getBundleID(), aPackage.getPlatform());
        if (app == null) {
            app = new com.kulian.Signalling.distribution.mode.App();
            String shortCode = CodeGenerator.generate(4);
            while (this.appDao.findByShortCode(shortCode) != null) {
                shortCode = CodeGenerator.generate(4);
            }
            BeanUtils.copyProperties(aPackage, app);
            app.setShortCode(shortCode);
        } else {
            app.setName(aPackage.getName());
            // 触发级联查询
            app.getPackageList().forEach(p->{});
            app.getWebHookList().forEach(webHook -> {});
        }
        if (app.getPackageList() == null) {
            app.setPackageList(new ArrayList<>());
        }
        return app;
    }


    public void deleteById(String id) {
       App app = this.appDao.findById(id).get();
        if (app != null) {
            this.appDao.deleteById(id);
            // 消除整个 APP 目录
            String path = PathManager.getAppPath(app);
            PathManager.deleteDirectory(path);
        }

    }

    /**
     * 通过 code 和 packageId 查询
     * @param code
     * @param packageId
     * @return
     */

    public AppViewModel findByCode(String code, String packageId) {
        com.kulian.Signalling.distribution.mode.App app = this.appDao.findByShortCode(code);
        AppViewModel viewModel = new AppViewModel(app, pathManager, packageId);
        return viewModel;
    }
}
